import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/components/HomeView.vue'
import TurtleSoupView from '@/components/TurtleSoupView.vue'
import LoginForm from '@/components/LoginForm.vue'
import UserProfileView from '@/components/UserProfileView.vue'
import ClassicSoupLibrary from '@/components/ClassicSoupLibrary.vue'
// 导入会员中心组件
import VipCenterView from '@/components/VipCenterView.vue'
import MessageCenterView from '@/components/MessageCenterView.vue'
import AchievementView from '@/components/AchievementView.vue'
import ContributeView from '@/components/ContributeView.vue'
import SupportView from '@/components/SupportView.vue'
import ShopView from '@/components/ShopView.vue'
// 导入充值页面组件（需要创建该组件）
import RechargeView from '@/components/RechargeView.vue'

const routes = [
  { path: '/', name: 'Home', component: HomeView },
  { path: '/game', name: 'Game', component: TurtleSoupView, meta: { requiresAuth: true } },
  { path: '/login', name: 'Login', component: LoginForm },
  { path: '/profile', name: 'Profile', component: UserProfileView, meta: { requiresAuth: true } },
  { path: '/classic-library', name: 'ClassicLibrary', component: ClassicSoupLibrary },
  // 新增会员中心路由
  { path: '/vip', name: 'VipCenter', component: VipCenterView, meta: { requiresAuth: true } },
  { path: '/messages', name: 'Messages', component: MessageCenterView, meta: { requiresAuth: true } },
  { path: '/achievement', name: 'Achievement', component: AchievementView, meta: { requiresAuth: true } },
  { path: '/contribute', name: 'Contribute', component: ContributeView, meta: { requiresAuth: true } },
  { path: '/support', name: 'Support', component: SupportView, meta: { requiresAuth: true } },
  { path: '/shop', name: 'Shop', component: ShopView, meta: { requiresAuth: true } },
  // 新增充值页面路由
  { path: '/recharge', name: 'Recharge', component: RechargeView, meta: { requiresAuth: true } }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫：验证登录状态
router.beforeEach((to, from, next) => {
  const isLoggedIn = !!localStorage.getItem('token')
  // 对需要登录的路由进行验证
  if (to.meta.requiresAuth && !isLoggedIn) {
    // 记录跳转前的路径，登录后可自动跳转回来
    next({ path: '/login', query: { redirect: to.fullPath } })
  } else {
    next()
  }
})

export default router
    